{% extends '../_manage.html' %}

{% block title %} {{ form.name }} {% endblock %}

{% block head %}

<script>

var SPONSORS = {{ sponsors|json|raw }};

var ADSLOTS;

$(function() {
    getJSON('/api/adSlots', function (err, data) {
        if (err) {
            return showError(err);
        }
        ADSLOTS = data.results;
        initVM({
        });
    });
});

function initVM() {
    var vm = new Vue({
        el: '#vm',
        data: {
            sponsors: SPONSORS,
            adSlots: ADSLOTS,
            adPeriod: {
            	userId: 0,
            	adSlotId: 0
            }
        },
        created: function () {
            $('#loading').hide();
            $('#vm').show();
        },
        methods: {
            submit: function() {
                clearFormError();
                this.$resource('/api/adPeriods').save(this.adPeriod).then(function (resp) {
                    resp.json().then(function (result) {
                        location.assign('/manage/ad/adperiod_list');
                    });
                }, onJsonFormError);
            }
        }
    });
}
</script>

{% endblock %}

{% block main %}

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> {{ _('Loading') }}...
    </div>

    <div id="vm" class="uk-width-1-1">
        <ul class="uk-tab" data-uk-tab>
            <li><a href="#0" onclick="location.assign('adslots_list')">{{ _('Ad Slots') }}</a></li>
            <li class="uk-active"><a href="#0">{{ _('Ad Periods') }}</a></li>
            <li><a href="#0" onclick="location.assign('admaterial_list')">{{ _('Ad Materials') }}</a></li>
        </ul>

        <form v-on:submit.prevent="submit" class="uk-margin uk-form uk-form-stacked">
            <legend>{{ _('New Ad Period') }}</legend>
            <fieldset>
                <div class="uk-alert uk-alert-danger uk-hidden"></div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Sponsor') }}:</label>
                    <div class="uk-form-controls">
                        <select v-model="adPeriod.userId" name="userId" class="uk-form-width-medium">
                            <option value="0">-- Select a sponsor --</option>
                            <option v-for="u in sponsors" v-bind:value="u.id" v-text="u.name"></option>
                        </select>
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Ad Slot') }}:</label>
                    <div class="uk-form-controls">
                        <select v-model="adPeriod.adSlotId" name="adSlotId" class="uk-form-width-medium">
                            <option value="0">-- Select a slot --</option>
                            <option v-for="s in adSlots" v-bind:value="s.id" v-text="s.name + ' ' + s.width + ' x ' + s.height"></option>
                        </select>
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Start at') }}:</label>
                    <div class="uk-form-controls">
                        <input v-model="adPeriod.startAt" name="startAt" type="date" class="uk-form-width-medium">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('End at') }} {{ _('Exclusive') }}:</label>
                    <div class="uk-form-controls">
                        <input v-model="adPeriod.endAt" name="endAt" type="date" class="uk-form-width-medium">
                    </div>
                </div>
                <div class="uk-form-row">
                    <div class="uk-form-controls">
                        <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-check"></i> Save</button>
                        <button type="button" class="uk-button" onclick="location.assign('/manage/ad/adperiod_list')"><i class="uk-icon-times"></i> Cancel</button>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>

{% endblock %}
